// app.js
const _api = require('./utils/api.js');
const apiurl = require('./utils/apiurl/public.js');
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    wx.getSystemInfo({
        success: e => {
          this.globalData.StatusBar = e.statusBarHeight;
          let capsule = wx.getMenuButtonBoundingClientRect();
          if (capsule) {
               this.globalData.Custom = capsule;
              this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
          } else {
              this.globalData.CustomBar = e.statusBarHeight + 50;
          }
        }
      })
      


      wx.login({
        success:res=>{

          var code=res.code
          if(code){
              wx.getUserInfo({
                success:ures=>{
                  _api.post(apiurl.login,{js_code:code,encryptedData:ures.encryptedData,iv:ures.iv}).then(loginres=>{
                      console.log(232323);
                      console.log(loginres.data);
                      wx.setStorageSync('token', loginres.data.data.token)
                      const obj = wx.getLaunchOptionsSync();
                        const query = obj.query; // 包含地址栏参数的对象
                        // console.log(query)
                        // console.log('启动小程序的路径:',obj.path)
                        // console.log('启动小程序的场景值:', obj.scene)
                        // console.log('启动小程序的 query 参数:', obj.query)
                        // console.log('来源信息:', obj.shareTicket)
                        // console.log('来源信息参数appId:', obj.referrerInfo.appId)
                        // console.log('来源信息传过来的数据:', obj.referrerInfo.extraData)
                        //index/user/data_report
                        let params={
                            clue_token:query.clue_token,
                            project_id:query.project_id,
                            promotion_id:query.promotion_id,
                            advertiser_id:query.advertiser_id,
                            req_id:query.req_id,
                            mid1:query.mid1,
                            mid2:query.mid2,
                            mid3:query.mid3,
                            mid4:query.mid4,
                            mid5:query.mid5,
                        }
                        if(params.clue_token && params.project_id){
                            _api.post('index/user/data_report',params).then(qdres=>{
                            })
                        }
                        //获取用户信息
                      _api.get(apiurl.userinfo).then(res=>{
                            if(res.data.code==0){

                                wx.setStorageSync('getuser', res.data.data)
                            }
                        })

                        


                    })
                }
              })
          }
        }
      })


      if (wx.canIUse('getUpdateManager')) {
        const updateManager = wx.getUpdateManager()
        updateManager.onCheckForUpdate(function (res) {
         if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
           wx.showModal({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success: function (res) {
             if (res.confirm) {
              updateManager.applyUpdate()
             }
            }
           })
          })
          updateManager.onUpdateFailed(function () {
           wx.showModal({
            title: '已经有新版本了哟~',
            content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
           })
          })
         }
        })
       } else {
        wx.showModal({
         title: '提示',
         content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
        })
       }
      


  },
  globalData: {
    userInfo: null,
    ColorList: [{
        title: '嫣红',
        name: 'red',
        color: '#e54d42'
      },
      {
        title: '桔橙',
        name: 'orange',
        color: '#f37b1d'
      },
      {
        title: '明黄',
        name: 'yellow',
        color: '#fbbd08'
      },
      {
        title: '橄榄',
        name: 'olive',
        color: '#8dc63f'
      },
      {
        title: '森绿',
        name: 'green',
        color: '#39b54a'
      },
      {
        title: '天青',
        name: 'cyan',
        color: '#1cbbb4'
      },
      {
        title: '海蓝',
        name: 'blue',
        color: '#0081ff'
      },
      {
        title: '姹紫',
        name: 'purple',
        color: '#6739b6'
      },
      {
        title: '木槿',
        name: 'mauve',
        color: '#9c26b0'
      },
      {
        title: '桃粉',
        name: 'pink',
        color: '#e03997'
      },
      {
        title: '棕褐',
        name: 'brown',
        color: '#a5673f'
      },
      {
        title: '玄灰',
        name: 'grey',
        color: '#8799a3'
      },
      {
        title: '草灰',
        name: 'gray',
        color: '#aaaaaa'
      },
      {
        title: '墨黑',
        name: 'black',
        color: '#333333'
      },
      {
        title: '雅白',
        name: 'white',
        color: '#ffffff'
      },
    ]
  }
})
